package DTO;

import java.util.ArrayList;

import database.DatabaseHelper;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class SignTypeDTO {
	
	//Properties
	private String ID;
	private String Name;
	
	
	//Property ID
	public String getID() { return ID; }
	public void setID(String id) { ID = id; }
	
	//Property Name
	public String getName() { return Name; }
	public void setName(String name) { Name = name; }
	//Methods
	
	public String GetID(int number)
	{
		String result = "ST";
		if(number<10)
		{
			result+="000"+number;
		}
		else if(number<100)
		{
			result+="00"+number;
		}else if(number<1000)
		{
			result+="0"+number;
		}else
		{
			result+=number;
		}
		return result;
	}
	
	public static SignTypeDTO getSignTypeByID(Context context, String ID){
		SignTypeDTO signTypeDTO = new SignTypeDTO();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
		
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	String query = "SELECT * FROM SignTypes WHERE ID = '" + ID +"'";
	 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
		if(cursor.moveToFirst()){
			signTypeDTO.setID(cursor.getString(cursor.getColumnIndex("ID")));
			signTypeDTO.setName(cursor.getString(cursor.getColumnIndex("Name")));
		}
		myDbHelper.close();
		return signTypeDTO;
	}
	
	public static ArrayList<SignTypeDTO> getAllSignTypeDTO(Context context){
		ArrayList<SignTypeDTO> result = new ArrayList<SignTypeDTO>();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	String query = "SELECT * FROM SignTypes";
	 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
		if(cursor.moveToFirst()){
			//Set value to properties
			do{
				SignTypeDTO signTypeDTO = new SignTypeDTO();
				signTypeDTO.setID(cursor.getString(cursor.getColumnIndex("ID")));
				signTypeDTO.setName(cursor.getString(cursor.getColumnIndex("Name")));
				result.add(signTypeDTO);
			}while(cursor.moveToNext());
		}
	 	myDbHelper.close();
	 	return result;
	}
}
